package cn.edu.hit.core.message;

import cn.edu.hit.HttpRequestorWithTokenForJoinsystem;
import cn.edu.hit.core.Document;
import cn.edu.hit.core.conf.ConfigureLoader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 获取钧安世通实时快数据
 */
public final class JoinSystemFastAdapter extends MessageAdapterHelper2<JSONArray> {


    @Override
    public boolean accept(String type) {
        return "JoinSystemFast".equalsIgnoreCase(type);
    }

    private static HttpRequestorWithTokenForJoinsystem requestor;
    private String token_aquire_url;
    private String token_aquire_user;
    private String token_aquire_password;
    private String feature_aquire_url;

    private static String urlString = "http://172.16.45.250:8094/rsget/realtimeData/points/";

    {
        Document conf = ConfigureLoader.getInstance().getBaseConfigure();
        urlString = conf.getString("JOINSYSTEM_GET_URL", "http://172.16.45.250:8094/rsget/realtimeData/points/");

        token_aquire_url = conf.getString("token_aquire_url", token_aquire_url);
        token_aquire_user = conf.getString("token_aquire_user", token_aquire_user);
        token_aquire_password = conf.getString("token_aquire_password", token_aquire_password);
        feature_aquire_url = conf.getString("feature_aquire_url", feature_aquire_url);
        requestor = HttpRequestorWithTokenForJoinsystem.getInstance(token_aquire_url, token_aquire_user, token_aquire_password);
    }

    @Override
    protected JSONArray pretreat(Long timestamp) {
        List<String> jsIds = configureMap.entrySet().parallelStream().map(entry -> {
            Document configure = (Document) entry.getValue();
            return configure.getString(DATA_SOURCE_ARGS);
        }).collect(Collectors.toList());

        if (jsIds.size() == 0)
            return new JSONArray();

        try {
            return requestAsJson(jsIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONArray();
    }

    @Override
    protected Object getValue(JSONArray data, String id, Document configure) {
        String joinSystemId = configure.getString(DATA_SOURCE_ARGS);
        Optional<JSONObject> optional = data.stream() //.map(x-> ((JSONObject) x))
                .filter(json -> joinSystemId.equals(((JSONObject) json).optString("pointId")))
                .findAny();
        if (!optional.isPresent()) {
            return Collections.emptyList();
        }

        String valueString = optional.get().getString("waveValues");
        if ("-".equals(valueString)) {
            return Collections.emptyList();
        } else {
            return Arrays.stream(valueString.split(","))
                    .mapToDouble(Double::parseDouble).boxed()
                    .collect(Collectors.toList());
//            return Collections.singletonList(Double.parseDouble(valueString));
        }
    }

    private static JSONArray requestAsJson(List<String> jsIds) throws Exception {
        String url = urlString + String.join(",", jsIds);
        String resultJson = requestor.doGet(url);
        System.out.println(resultJson);
        JSONObject jsonObject = JSONObject.fromObject(resultJson);
        return jsonObject.getJSONArray("data");
    }

    public static void main(String[] args) throws Exception {
        System.out.println(requestAsJson(Arrays.asList("1", "4")));
    }
}
